#include <unistd.h>
#include <sys/socket.h>
#include <iostream>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>

using namespace std;

static const int PORT = 50000;

int main(int argc, char **argv)
{
	int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	int psize;
	int waitTime; /*micro seconds*/
	int pkts;
	int isPrint;

	if(argc < 4)
	{
		cout<<"arg must be more than 1"<<endl;
	}

	struct sockaddr_in addr;
	bzero(&addr, sizeof(struct sockaddr_in));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(PORT);
	addr.sin_addr.s_addr = inet_addr(argv[1]);

	pkts = atoi(argv[2]);
	waitTime = atoi(argv[3]);
	isPrint = atoi(argv[4]);

	/*
	if(argc < 5)
	{
		cout<<"arg is less than 4"<<endl;
	}
	
	psize = atoi(argv[1]);
	waitTime = atoi(argv[2]);
  pkts = atoi(argv[3]);

	char *buff = (char *)malloc(sizeof(char) * psize);
	*/

	char buff[1024];
	//int i = 0;
	while(pkts > 0)
	{
		cout<<pkts<<endl;
		sendto(sockfd, buff, sizeof(buff), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr));
		if(waitTime != 0)
			usleep(waitTime);
		pkts--;
		if(isPrint == 1)
			cout<<pkts<<endl;
	}
}
